package cn.com.surker.material.service.impl;

import cn.com.surker.dto.main.MainMaterialLimitDTO;
import cn.com.surker.material.dao.MainMaterialLimitDao;
import cn.com.surker.material.entity.MainMaterialLimit;
import cn.com.surker.material.service.MainMaterialLimitService;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.main.MainMaterialLimitVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.snaker.engine.SnakerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
@Slf4j
public class MainMaterialLimitServiceImpl  extends ServiceImpl<MainMaterialLimitDao, MainMaterialLimit> implements MainMaterialLimitService {


    @Resource
    private MainMaterialLimitDao mainMaterialLimitDao;


    @Override
    public IPage<MainMaterialLimitVO> findPage(MainMaterialLimitDTO dto) {
        PageHelper.offsetPage(dto.getOffsetSize(), dto.getLimit());
        Page<MainMaterialLimitVO> page = new Page<>(dto.getCurrSize(), dto.getLimit());
        IPage<MainMaterialLimitVO> iPage = mainMaterialLimitDao.findPage(page, dto);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    @Override
    public int saveMaterialLimit(MainMaterialLimitDTO dto, CacheUser user) {
        MainMaterialLimit mainMaterialLimit = BeanUtil.copyProperties(dto, MainMaterialLimit.class);
        mainMaterialLimit.setStatus(0);
        mainMaterialLimit.setCreateTime(LocalDateTime.now());
        mainMaterialLimit.setCreator(user.getName());
        return mainMaterialLimitDao.insert(mainMaterialLimit);
    }

    @Override
    public int updateMaterialLimit(MainMaterialLimitDTO dto, CacheUser user) {
        MainMaterialLimit dbLimit = mainMaterialLimitDao.selectById(dto.getId());
        if(ObjectUtil.isEmpty(dbLimit)){
            throw new SnakerException("当前数据不存在不允许修改！");
        }
        BeanUtil.copyProperties(dto,dbLimit);
        dbLimit.setUpdateTime(LocalDateTime.now());
        dbLimit.setUpdator(user.getName());
        return mainMaterialLimitDao.updateById(dbLimit);
    }

    @Override
    public int updateStatus(Long id, Integer status, CacheUser user) {
        MainMaterialLimit dbLimit = mainMaterialLimitDao.selectById(id);
        if(ObjectUtil.isEmpty(dbLimit)){
            throw new SnakerException("当前数据不存在不允许修改！");
        }
        dbLimit.setStatus(status);
        dbLimit.setUpdateTime(LocalDateTime.now());
        dbLimit.setUpdator(user.getName());
        return mainMaterialLimitDao.updateById(dbLimit);
    }
}
